﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using OnlineManagement.IDAL;
using OnlineManagement.DBUtility;//Please add references
namespace OnlineManagement.SQLServerDAL.Dal
{
	/// <summary>
	/// 数据访问类,母猪分娩记录表PropagateRecord
	/// </summary>
	public partial class PropagateRecord:IPropagateRecord
	{
        public PropagateRecord()
        { }
        #region  BasicMethod
        
        /// <summary>
        /// 添加母猪繁殖记录
        /// </summary>
        /// <param name="model"></param>
        /// <param name="pigTypeId"></param>
        /// <param name="returnValue"></param>
        /// <returns></returns>
        public bool AddSowPropagateRecord(OnlineManagement.Model.PropagateRecord model, out string returnValue)
        {
            returnValue = "";
            int rowsAffected=0;
            if (model == null)
            {
                returnValue = "提交的信息不能为空";
                return false;
            }
            string proceName = Common.PubProcedures.SowPropagateRecordAddProce;
            SqlParameter[] paras = { 
                    new SqlParameter("@HoggeryID", SqlDbType.Int,4),
					new SqlParameter("@SowID", SqlDbType.UniqueIdentifier,16),
                    new SqlParameter("@PropagateDate", SqlDbType.Date),
                    new SqlParameter("@ChildPigTypeId", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@HealthCount", SqlDbType.TinyInt,1),
					new SqlParameter("@WeakCount", SqlDbType.TinyInt,1),
					new SqlParameter("@DeathCount", SqlDbType.TinyInt,1),
					new SqlParameter("@NestWeight", SqlDbType.Float),
					new SqlParameter("@MaxChildWeight", SqlDbType.Float),
					new SqlParameter("@MinWeight", SqlDbType.Float),
                    new SqlParameter("@KeepChildTotal",SqlDbType.TinyInt,1),
					new SqlParameter("@Comment", SqlDbType.Text),
                    new SqlParameter("@msg",SqlDbType.NVarChar,300),
                    new SqlParameter("@rowsAffected",SqlDbType.TinyInt,1)
                                   };
            paras[0].Value = model.HoggeryID;
            paras[1].Value = model.SowID;
            paras[2].Value = model.PropagateDate;
            paras[3].Value = model.ChildPigTypeId;
            paras[4].Value = model.HealthCount;
            paras[5].Value = model.WeakCount;
            paras[6].Value = model.DeathCount;
            paras[7].Value = model.NestWeight;
            paras[8].Value = model.MaxChildWeight;
            paras[9].Value = model.MinChildWeight;
            paras[10].Value = model.KeepChildTotal;
            paras[11].Value = model.Comment;
            paras[12].Direction = ParameterDirection.Output;
            paras[13].Direction = ParameterDirection.Output;
            try
            {
                DbHelperSQL.RunProcedure(proceName, paras, out rowsAffected, out returnValue);
                return rowsAffected > 0 ? true : false;
            }
            catch (Exception ex) { throw ex; }
        }
        

		#endregion  BasicMethod

		#region  ExtensionMethod
        /// <summary>
        /// sow繁殖
        /// </summary>
        /// <param name="model"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public int SowPropagate(OnlineManagement.Model.PropagateRecord model, out string message) {
            message = string.Empty;

            string proceName = OnlineManagement.Common.PubProcedures.SowPropagateProce;
            SqlParameter[] parameters = {
					new SqlParameter("@HoggeryID", SqlDbType.Int,4),
					new SqlParameter("@SowID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@PropagateDate", SqlDbType.Date,3),
					new SqlParameter("@LiveCount", SqlDbType.TinyInt,1),
					new SqlParameter("@DeathCount", SqlDbType.TinyInt,1),
					new SqlParameter("@MalformationCount", SqlDbType.TinyInt,1),
					new SqlParameter("@WeakCount", SqlDbType.TinyInt,1),
					new SqlParameter("@MaleCount", SqlDbType.TinyInt,1),
					new SqlParameter("@FemaleCount", SqlDbType.TinyInt,1),
                    new SqlParameter("@Comment", SqlDbType.Text),
                    new SqlParameter("@msg",SqlDbType.NVarChar,200),
                    new SqlParameter("@rowsAffected",SqlDbType.TinyInt,1)
                                        };
            //parameters[0].Value = model.HoggeryID;
            //parameters[1].Value = model.SowID;
            //parameters[2].Value = model.PropagateDate;
            //parameters[3].Value = model.LiveCount;
            //parameters[4].Value = model.DeathCount;
            //parameters[5].Value = model.MalformationCount;
            //parameters[6].Value = model.WeakCount;
            //parameters[7].Value = model.MaleCount;
            //parameters[8].Value = model.FemaleCount;
            //parameters[9].Value = model.Comment;
            parameters[10].Direction = ParameterDirection.Output;
            parameters[11].Direction = ParameterDirection.Output;
            int rowAffected;
            DBUtility.DbHelperSQL.RunProcedure(proceName,parameters,out rowAffected,out message);
            return rowAffected;
        }

        /// <summary>
        /// 获取可繁殖的母猪信息
        /// </summary>
        /// <param name="hoggeryId"></param>
        /// <returns></returns>
        public System.Data.DataSet GetPropagateSow(int hoggeryId)
        {
            try
            {
                string procName = Common.PubProcedures.GetPropagateSowProc;
                SqlParameter[] paras = { new SqlParameter("@hoggeryId", SqlDbType.Int, 4) };
                paras[0].Value = hoggeryId;
                System.Data.DataSet ds = DbHelperSQL.RunProcedure(procName, paras);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("数据库连接有误，请联系管理员。");
            }
        }

        /// <summary>
        /// 获取指定母猪的
        /// </summary>
        /// <param name="hoggeryId"></param>
        /// <param name="sowId"></param>
        /// <returns></returns>
        public DataSet GetSpecificSowChildInfo(int hoggeryId,Guid sowId) {
            try
            {
                string query = "SELECT TOP 1 pb.TypeName,pr.PropagateDate, pr.KeepChildTotal,pr.RecordID as PropagateRecordId,pr.HybrizationRecord FROM PropagateRecord pr LEFT JOIN PigBreed pb on pr.ChildPigTypeId=pb.TypeID WHERE pr.SowID=@sowId AND pr.HoggeryID=@hoggeryId ORDER BY pr.PropagateDate DESC";

                SqlParameter[] paras = { new SqlParameter("@sowId",SqlDbType.UniqueIdentifier,16),
                                       new SqlParameter("@hoggeryId", SqlDbType.Int, 4) };
                paras[0].Value = sowId;
                paras[1].Value = hoggeryId;
                DataSet ds = DbHelperSQL.Query(query, paras);
                return ds;
            }
            catch (Exception ex) { throw ex; }
        }
		#endregion  ExtensionMethod

        #region 不再使用
        /// <summary>
        /// 获取母猪猪圈名称及母猪饲养员姓名
        /// </summary>
        /// <param name="hoggeryId"></param>
        /// <param name="sowId"></param>
        /// <param name="pigTypeName">返回母猪饲养员姓名</param>
        /// <returns>返回母猪猪圈名称</returns>
        public string GetSowPigPenName(int hoggeryId, Guid sowId, out string feederName)
        {
            feederName = Common.PubConstant.EmptyString;
            string pigPenName = Common.PubConstant.EmptyString;
            string procName = string.Empty;
            SqlDataReader reader = null;
            try
            {
                IDataParameter[] parameters = new IDataParameter[] 
            { new SqlParameter("@hoggeryId", SqlDbType.Int, 4),
                new SqlParameter("@sowId",SqlDbType.UniqueIdentifier,16)
            };
                parameters[0].Value = hoggeryId;
                parameters[1].Value = sowId;
                reader = DbHelperSQL.RunProcedure(procName, parameters);
                while (reader.Read())
                {
                    pigPenName = reader[0].ToString();
                    feederName = string.IsNullOrEmpty(reader[1].ToString()) ? Common.PubConstant.EmptyString : reader[1].ToString();
                }
                return pigPenName;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (reader != null) { reader.Close(); }
            }
        }
        #endregion
    }
}

